require(['grid', 'topBar', 'layout'], function () {
    var editDialog,
        $top = $('#top'),
        $grid = $('#grid');

    $('#top').topBar({
        left: [
            {
                text: '查询',
                type: 'btn',
                click: function () {
                    var carIds = getLeftTreeSelectCarIds();
                    if (carIds) {
                        $grid.grid('setGridParam', {
                            datatype: "JSON",
                            page: 1,
                            postData: {orgIds: carIds}
                        }).trigger("reloadGrid");
                    }
                }
            },
            {
                text: '绑定',
                type: 'btn',
                click: function () {
                    var carIds = getGridSelectCarIds(false);
                    if (carIds) {
                        getEditDialog().dialog('exec', 'loadData', carIds);
                    }
                }
            },
            {
                text: '解绑',
                type: 'btn',
                click: function () {
                    var carIds = getGridSelectCarIds(true);
                    if (carIds) {
                    	$.messager.confirm('删除', '确定要解绑所选车辆的电子围栏吗？', 'delete', function (r) {
                    		if(r){
                    			unbind(carIds);
                    		}
                    	});
                    }
                }
            }
        ]
    });

    $('body').layout();

    function getLeftTreeSelectCarIds() {
        var carIds = commonFun.getLeftTreeCheckedNodes('CAR');
        if (carIds && carIds.length) {
            return carIds;
        } else {
            $.messager.alert("提示", "请选择车辆", "info");
        }
    }

    //获取grid勾选行的车辆ID（以数组的方式返回）
    function getGridSelectCarIds(binding) {
        var row = $grid.grid('getSelections');
        var ids = [];
        if (row.length) {
            $.each($grid.grid('getSelections'), function (i, item) {
                if(binding){
                    if(item.relSt=="绑定中"){
                        ids.push(item.carId);
                    }
                }
                else{
                    ids.push(item.carId);
                }
            });

        } else {
            $.messager.alert("提示", "请先选择要操作的车辆", "info");
            return;
        }
        return ids;
    }

    //解绑
    function unbind(carIds) {
        if(!$.isEmptyObject(carIds))
        {
        	
        	$.ajaxExt({
        		url: '/electFencSet/unsetElectFenc.htm',
        		data: {
        			orgIds: carIds
        		}
        	}).done(function () {
        		$grid.trigger("reloadGrid");
        	});
        	
        }
        else{
            $.messager.alert("提示", "请选择绑定中的车辆", "info");
        }

    }

    function getOperate(data){
        var result;
        if(data&&data.fencName1){
            result=[{
                text: '编辑',
                css: 'operate-edit',
                click: function (data) {
                    getEditDialog().dialog('exec', 'loadData', data.carId);
                }
            }, {
                text: '解绑',
                css: 'operate-delete',
                click: function (data) {
                    $.messager.confirm('解绑', '确定要解绑该车辆的所有电子围栏吗？', 'delete', function (r) {
                        if (r) {
                            unbind([data.carId])
                        }
                    });
                }
            }];
        }
        else{
            result=[{
                text: '编辑',
                css: 'operate-edit',
                click: function (data) {
                    getEditDialog().dialog('exec', 'loadData', data.carId);
                }
            }];
        }
        return result;
    }

    $grid.grid({
        url: "/electFencSet/searchElectFencCarRel.htm",
        colModel: [
            {name: "carId", label: 'carId', key: true, hidden: true},
            {name: "licPltNo", label: '车牌号'},
            {name: "lineName", label: '线路'},
            {name: "organName", label: '所属机构'},
            {name: "fencName1", label: '当前电子围栏'},
            {name: "fencName2", label: '下发电子围栏'},
            {name: "relSt", label: '状态'},
            {
                name: "operate", width: 130, label: '操作', title: false, align: 'center',
                formatter: 'operArea',
                formatoptions: [{
                    text: '编辑',
                    css: 'operate-edit',
                    click: function (data) {
                        getEditDialog().dialog('exec', 'loadData', data.carId);
                    }
                }, {
                    text: '解绑',
                    css: 'operate-delete',
                    rule: function (data) {
                        return data.fencName1 != undefined
                    },
                    click: function (data) {
                        $.messager.confirm('解绑', '确定要解绑该车辆的所有电子围栏吗？', 'delete', function (r) {
                            if (r) {
                                unbind([data.carId])
                            }
                        });
                    }
                }]
            }
        ]
    });

    /**
     * 获取表单对话框，如果没有就先创建
     * @param option {object} 对话框配置
     * @returns {jQuery}
     */
    function getEditDialog() {
        if (editDialog) {
            editDialog.dialog('open');
        } else {
            editDialog = $('<div>').dialog({
                title: '电子围栏选择',
                url: './service/setting/funElectronFenceIssuedEdit.htm',
                height: 580,
                width: 480,
                autoOpen: true,
                close: function () {
                    $(this).dialog('exec', 'clear');
                },
                buttons: [
                    {
                        text: '确定',
                        css: 'mr10',
                        click: function () {
                            var _t = this;
                            $(_t).dialog('exec', 'issue', function () {
                                $grid.trigger("reloadGrid");
                                $(_t).dialog('close');
                            });
                        }
                    },
                    {
                        text: '关闭',
                        css: 'btn-cancel',
                        click: function () {
                            $(this).dialog('close');
                        }
                    }
                ]
            });
        }
        return editDialog;
    }
});
